/**
 * 
 */
package br.com.lazyprogrammer.worldsoccermanager.network.task;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;

import br.com.lazyprogrammer.worldsoccermanager.network.JSONConstants;
import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.URLParams;
import br.com.lazyprogrammer.worldsoccermanager.network.builders.URLBuilder;
import br.com.lazyprogrammer.worldsoccermanager.network.interfaces.StaffListener;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Staff;
import br.com.lazyprogrammer.worldsoccermanager.util.Parser;

/**
 * 
 * @author Limao Games <br />
 * <b>Email:</b> andreikeda@limaogames.com.br <br />
 * created on: May 24, 2014
 * @version 1.0
 * @see 
 */
public class GetStaffTask extends AsyncTask<String, Void, Request.Response> {
	
	private Request.Callback callback;
	private Context context;
	private StaffListener listener;
	
	public GetStaffTask(Context context, Request.Callback callback, StaffListener listener) {
		this.callback	= callback;
		this.context	= context;
		this.listener	= listener;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		callback.onStart(getClass());
	}

	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
	 */
	@Override
	protected Request.Response doInBackground(String... param) {
		// TODO Auto-generated method stub
		String id	= param[0];
		Parser parser	= new Parser();
		Request.Response status;
		
		try {
			JSONObject parent	= new Request().post(new URLBuilder().buildUrlGame(URLBuilder.Type.Team), new URLParams().buildGetTeamParams(id));
			status	= Request.Response.values()[parser.parseResponse(parent)];
			if (status.equals(Request.Response.Success)) {
                JSONArray array = parent.getJSONArray(JSONConstants.LIST);
                List<Staff> staffList = new ArrayList<Staff>();
                for (int i = 0; i < array.length(); i++)
                    staffList.add(parser.parseStaff(array.getJSONObject(i).optJSONObject(JSONConstants.STAFF)));
                listener.onStaffLoaded(staffList);
            }
        } catch (SocketTimeoutException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.SocketTimeout;
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.UnsupportedEncoding;
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.ClientProtocol;
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IllegalState;
			e.printStackTrace();
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.NullPointer;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.IO;
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			status	= Request.Response.JSON;
			e.printStackTrace();
		}
		
		return status;
	}

}
